﻿    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kardex_Entity;
using Kardex_BussinesLogic;
using Kardex_Common;
using KardexSist.UserControls.Common;
using KardexSist.Library;

namespace KardexSist.UserControls.Distributor
{
    public partial class CtrlDistributor : UserControl, IOperations
    {
        private person personCurrent;         
        private DistribuidorFactory distributorFc;

        #region ThrowEvents
        public delegate void ThrowListEventHandler();
        public event ThrowListEventHandler ShowListClick;

        protected virtual void OnShowListClick()
        {
            if (ShowListClick != null)
            { ShowListClick(); }
        }


        #endregion
        public CtrlDistributor()
        {
            InitializeComponent();
            personCurrent = new person() { distribuidor = new distribuidor() };
            
            OnShowListClick();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CtrlDistributor_Load(object sender, EventArgs e)
        {
            OnShowListClick();
        }


        public void TrigerObjPerson(person objperson)
        {
            personCurrent = new person() { distribuidor = new distribuidor() };
            personCurrent = objperson;
            LoadObjectPerson();
        }
        private void LoadObjectPerson()
        {
            try
            { 
                txtCity.Text = personCurrent.IdCD;
                txtCi.Text = personCurrent.CI;
                txtName.Text = personCurrent.Name;
                txtPhone.Text = personCurrent.Telephone;
                cmbCIExpedido.SelectedItem = personCurrent.IdTipoCIExpedido;
                txtNameConductor.Text = personCurrent.distribuidor.Chofer;

                if (personCurrent.Activ == true)
                {
                    chkActive.Checked = true;
                }
                else
                {
                    chkActive.Checked = false;
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        private void LoadData()
        {
            personCurrent.IdCD = txtCity.Text;
            personCurrent.CI = txtCi.Text;
            personCurrent.IdTipoCIExpedido = cmbCIExpedido.Text;
            personCurrent.Name = txtName.Text;
            personCurrent.distribuidor.Chofer = txtNameConductor.Text;
            personCurrent.Telephone = txtPhone.Text;
            personCurrent.Activ = chkActive.Checked;
        }

        private void ClearData()
        { 
            txtCity.Text = string.Empty;
            txtCi.Text = string.Empty; 
            txtName.Text = string.Empty;
            txtNameConductor.Text = string.Empty;
            txtPhone.Text = string.Empty;
            chkActive.Checked = false;
        }

        public void Add()
        {
            ClearData();
            personCurrent = new person() { distribuidor = new distribuidor() };
        }

        public void Delete()
        {
            try
            {
                var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionDelete, null);
                if (result == DialogResult.Yes)
                {
                    distributorFc = new DistribuidorFactory();
                    if (distributorFc.Delete(personCurrent) == true)
                    {
                        OnShowListClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Delete, null);
                        ClearData();
                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Save()
        {
            try
            { //insert
                if (personCurrent.IdPerson <= 0)
                {
                    LoadData();
                    distributorFc = new DistribuidorFactory();
                    personCurrent = distributorFc.Save(personCurrent); 
                    if (personCurrent.IdPerson > 0)
                    {
                        OnShowListClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Insert, null);

                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
                // update
                else
                {
                    var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionModif, null);
                    if (result == DialogResult.Yes)
                    {
                        LoadData();
                        distributorFc = new DistribuidorFactory();
                        if (distributorFc.Update(personCurrent) == true)
                        {
                            OnShowListClick();
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Update, null);
                        }
                        else
                        {
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                        }
                    }
                    else
                    {
                        ClearData();
                        LoadObjectPerson();
                    }

                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Export()
        {

        }
    }
}
